package org.unt.url;

import java.io.IOException;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.unt.logic.Strings;

public class Site {

	public static void main(String[] args) throws ClientProtocolException, IOException {

		// Site site1 = new Site("jip.inetpsa.com", 80, "http");
		// String pageContents = site1.fetchPageContents("/fr/description.html");
		// System.out.println(pageContents);

		// Site site1 = new Site("maven.apache.org", 80, "http", "http.internetpsa.inetpsa.com", 80, "u336654", "vago7930");
		// String pageContents = site1.fetchPageContents("/");
		// System.out.println(pageContents);

		// System.out.println(page.getContents());

		Site site1 = new Site("portail.inetpsa.com", 80, "http");
		System.out.println(site1.fetchPageContents("/poe/html/static/Accueil.html"));
		//
		// Site site2 = new Site("www.clarin.com", 80, "http", "http.internetpsa.inetpsa.com", 80, "u336654", "vago7930");
		// System.out.println(site2.fetchPageContents("/politica/Cassanello-Camara-Federal-tribunal-quedara_0_902909924.html"));

		// site1.shutdown();
	}

	private DefaultHttpClient httpclient = new DefaultHttpClient();
	private HttpHost targetHost = null;

	public Site(String host, int port, String scheme) {
		targetHost = new HttpHost(host, port, scheme);
	}

	public Site(String host, int port, String scheme, String proxyUrl, int proxyPort, String proxyUser, String proxyPassword) {
		targetHost = new HttpHost(host, port, scheme);
		httpclient.getCredentialsProvider().setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
		HttpHost proxy = new HttpHost(proxyUrl, proxyPort);
		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
	}

	public void shutdown() {
		httpclient.getConnectionManager().shutdown();
	}

	/**
	 * Gets a Page object
	 * 
	 * @param targetPath
	 *            If the URL is 'http://www.clarin.com/noticias/abc.html', this parameter is 'noticias/abc.html'
	 * @return A Page object
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	// public Page fetchPage(String targetPath) throws ClientProtocolException, IOException {
	// HttpGet httpGet = new HttpGet(targetPath);
	// HttpResponse response = httpclient.execute(targetHost, httpGet);
	// HttpEntity entity = response.getEntity();
	// String pageContents = EntityUtils.toString(entity);
	// return new Page(httpGet.getURI().toString(), pageContents);
	// }

	public String fetchPageContents(String relativePathPage) throws ParseException, IOException {

		HttpGet httpget = null;
		try {
			httpget = new HttpGet(relativePathPage);
		} catch (java.lang.IllegalArgumentException e) {
			System.out.println("ERROR : IllegalArgumentException : " + e.getMessage());
			return "ERROR : IllegalArgumentException : " + e.getMessage();
		}

		HttpEntity entity = null;

		try {

			HttpResponse response = httpclient.execute(targetHost, httpget);

			entity = response.getEntity();

			// entity.getContentLength());
			// EntityUtils.consume(entity);

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return EntityUtils.toString(entity);
	}
}
